/**
 * Created by cchen on 2015/5/19.
 */


'use strict';
angular.module('backofficeApp.retailerExchange.controller', []).
    controller('retailerExchangeCtrl', ['$scope', '$http', function ($scope, $http) {
        $scope.$watch('$viewContentLoaded', function () {
            $scope.voucher_status = [
                {value: "", name: "全部"},
                {value: "0", name: "未结算"},
                {value: "1", name: "已结算"}
            ]
            $scope.filter_retailer_name = "";
            $scope.filter_status = "";
            $scope.filter_retailer_code = "";
        });

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                });

            }

            d.prototype.getValue = function (isFrom) {

                if ($(id).val() == "") {
                    return undefined;
                }

                if (!this.date) {
                    return undefined
                }

                if (isFrom) {

                    return this.date.setHours(0, 0, 0, 0)
                }
                return this.date.setHours(23, 59, 59, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.startDate = datePicker("#startDate");
        $scope.endDate = datePicker("#endDate");
        $scope.exStartDate = datePicker("#exStartDate");
        $scope.exEndDate = datePicker("#exEndDate");
        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        $scope.filter = function () {
            var params = {
                status: $scope.filter_status,
                name: $scope.filter_retailer_name,
                code: $scope.filter_retailer_code,
                cardNo: $scope.filter_card_no,
                fromDate: $scope.startDate.getValue(true),
                toDate: $scope.endDate.getValue(false),
                exFromDate: $scope.exStartDate.getValue(true),
                exToDate: $scope.exEndDate.getValue(false),
                currentPage: $scope.currentPage
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/RetailerExchangeList', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.resultList = data.data.data;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
            });
        }

        $scope.export = function () {

            var href = 'http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/exportRetailerExchangeList?1=1';

            if ($scope.filter_status != null && $scope.filter_status != '') {
                href += "&status=" + $scope.filter_status;
            }
            if ($scope.filter_retailer_name != null && $scope.filter_retailer_name != '') {
                href += "&name=" + $scope.filter_retailer_name;
            }
            if ($scope.filter_retailer_code != null && $scope.filter_retailer_code != '') {
                href += "&code=" + $scope.filter_retailer_code;
            }
            if ($scope.filter_card_no != null && $scope.filter_card_no != '') {
                href += "&cardNo=" + $scope.filter_card_no;
            }
            if ($scope.startDate.getValue(true) != null) {
                href += "&fromDate=" + $scope.startDate.getValue(true);
            }
            if ($scope.endDate.getValue(false) != null ) {
                href += "&toDate=" + $scope.endDate.getValue(false);
            }
            if ($scope.exStartDate.getValue(true) != null) {
                href += "&exFromDate=" + $scope.exStartDate.getValue(true);
            }
            if ($scope.exEndDate.getValue(false) != null) {
                href += "&exToDate=" + $scope.exEndDate.getValue(false);
            }

            location.href = href;
        };

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.filter();
        }

        $scope.filter();
    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    });